<template>
	<view class="ys">
		<view class="userlist">
			<view class="useritem" v-for="(item,index) in userlist" :key="index" @click="shut(index)">
				<image :src="item.head_url|getImg" mode=""></image>
				<view class="name">
					{{item.name || item.real_name || item.username}}
				</view>
				<view :class="showBtn == index?'btns active':'btns'">
					<text class="on" @click.stop="setAllowSay(item,index,`确定要禁言${item.name}吗`)"
						v-show="item.allow_say == 1">禁言</text>
					<text class="off" @click.stop="setAllowSay(item,index,`确定要取消禁言${item.name}吗`)"
						v-show="item.allow_say == 0">取消禁言</text>
				</view>
			</view>
			<!-- <view class="useritem">
				<image src="/static/icon/more.png" mode="widthFix"></image>
				<view class="name">
					查看更多
				</view>
			</view> -->

		</view>
	</view>
</template>

<script>
	// import {
	// 	getChatUser,
	// 	setAllowSay
	// } from "@/api/user.js"
	export default {
		data() {
			return {
				room_id: '',
				userlist: [],
				showBtn: -1,
				user_id: '',
				allow_set: 0
			}
		},
		onLoad(options) {
			if (options.room_id) {
				this.room_id = options.room_id
				this.getChatUser()
			}
		},
		methods: {
			shut(index) {
				if (!this.allow_set) {
					return false
				}
				if (this.userlist[index].user_id == this.user_id) {
					this.showBtn = -1
					return false
				}
				if (this.showBtn == index) {
					this.showBtn = -1
				} else {
					this.showBtn = index
				}
			},
			getChatUser() {
				getChatUser({
					room_id: this.room_id
				}).then(res => {
					this.userlist = res.data.list
					this.allow_set = res.data.allow_set
					this.user_id = res.data.user_id
				})
			},
			setAllowSay(item, index, tips) {
				let that = this
				that.showBtn = -1
				console.log(item, Number(!item.allow_set))
				uni.showModal({
					title: '温馨提示',
					content: tips,
					success: (res) => {
						if (res.confirm) {
							setAllowSay({
								room_id: that.room_id,
								user_id: item.user_id
							}).then(res => {
								uni.showToast({
									title: res.message,
									icon: 'none'
								})
								if (res.status == 1) {
									setTimeout(() => {
										that.$nextTick(() => {
											// let list = that.userlist
											// list[index].allow_say = Number(!
											// 	item.allow_say)
											this.$set(item, 'allow_say', Number(!item
												.allow_say))
										})
										console.log(that.userlist);
									}, 1000)
								} else {

								}
							})
						}
					}
				})

			}
		}
	}
</script>

<style lang="scss">
	.userlist {
		background: #fff;
		display: flex;
		flex-wrap: wrap;
		padding: 10px;

		.useritem {
			width: 20%;
			padding: 0 5px;
			box-sizing: border-box;
			margin-bottom: 15px;
			text-align: center;
			position: relative;

			.btns {
				position: absolute;
				left: 100%;
				top: 0;
				width: 0;
				display: flex;
				flex-direction: column;
				text-align: center;
				background: #fff;
				z-index: 111;
				border-radius: 5px;
				font-size: 0;
				overflow: hidden;
				box-shadow: 0 0 15px #ccc;
				transition: .2s;
				white-space: nowrap;

				.on {
					background: red;
					color: #fff;
					padding: 8px 0;
				}

				.off {
					background: #fff;
					color: #777;
					padding: 8px 0;
				}
			}

			.active {
				width: 100%;
				font-size: 14px;
			}

			image {
				width: 40px;
				height: 40px;
				border-radius: 5px;
			}

			.name {
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
				font-size: 13px;
			}
		}

		.useritem:nth-child(4n),
		.useritem:nth-child(5n) {
			.btns {
				left: unset;
				right: 100%;
			}
		}
	}
</style>